﻿using System;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
using UniMana.Data;
using UniMana.Utils.Controls;


namespace UniMana.Win.Training
{
    public partial class FrmCopyTrainingProgram : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_dbTraining = new DBMain();
        private bds m_bds = new bds();
        private Training_TrainingProgramInfo m_trainingProgramInfo;
        public FrmCopyTrainingProgram()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            this.lueCatalogue_Course.GetData(this.m_dbTraining.m_DCReadOnly.Catalogue_Courses.GetYearLasted(5));
            this.lueCatalogue_Department.GetData(this.m_dbTraining.m_DCReadOnly.Catalogue_Departments.GetDepartmentStudentManage());
            this.lueCatalogue_Level.GetData(this.m_dbTraining.m_DCReadOnly.Catalogue_Levels);
            this.lueCatalogue_Major.GetData(this.m_dbTraining.m_DCReadOnly.Catalogue_Majors);
            this.lueCatalogue_TrainingType.GetData(this.m_dbTraining.m_DCReadOnly.Catalogue_TrainingTypes);
            this.lueCatalogue_CourseNew.GetData(this.m_dbTraining.m_DCReadOnly.Catalogue_Courses.GetYearLasted(1),"Catalogue_CourseKey","Name");
        }

        private Training_TrainingProgramInfo GetTrainingProgram()
        {
            int iCourseKey = this.lueCatalogue_Course.GetIntValue();
            int iDepartmentKey = this.lueCatalogue_Department.GetIntValue();
            int iLevel = this.lueCatalogue_Level.GetIntValue();
            int iMajor = this.lueCatalogue_Major.GetIntValue();
            int iTrainingType = this.lueCatalogue_TrainingType.GetIntValue();
            if (iCourseKey != -1 && iDepartmentKey != -1 && iTrainingType != -1 && iMajor != -1)
            {
                this.m_trainingProgramInfo = new Training_TrainingProgramInfo(iMajor, iLevel, iTrainingType, iCourseKey);
                return this.m_trainingProgramInfo;
            }
            else
            {
                return null;
            }
        }
        private int GetMajorKey()
        {
            groupControl2.Enabled = false;
            int iDepartmentKey = this.lueCatalogue_Department.GetIntValue();
            int iLevelKey = this.lueCatalogue_Level.GetIntValue();
            Catalogue_MajorInfo majorInfo = new Catalogue_MajorInfo(iLevelKey, iDepartmentKey);
            if (majorInfo.ExistInDataBase)
            {
                return majorInfo.Properties.Catalogue_MajorKey;
            }
            else
            {
                return 0;
            }
        }

        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {
            this.lueCatalogue_Major.EditValue = GetMajorKey();
        }

        private void lueCatalogue_Course_EditValueChanged(object sender, EventArgs e)
        {
            this.lueCatalogue_Major.EditValue = GetMajorKey();
        }

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            this.lueCatalogue_Major.EditValue = GetMajorKey();
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //////Hỏi lại Hòa về lỗi
            //////Training_TrainingProgramInfo info = GetTrainingProgram();
            //////if (info != null)
            //////{
            //////    if (info.ExistInDatabase)
            //////    {
            //////        groupControl2.Enabled = true;
            //////        this.txtID.Text = info.Properties.ID;
            //////        this.txtName.Text = info.Properties.Name;
            //////    }
            //////}
        }

        private void btnAgree_Click(object sender, EventArgs e)
        {
            CopyTrainingProgram();
        }

        private void CopyTrainingProgram()
        {
            ///////Hỏi lại Hòa về lỗi
            /*-------string strID = this.txtIDNew.Text;
            if (strID == "")
            {
                XtraMessageBox.Show("Mã chương trình đào tạo không thể bỏ trống");
                return;
            }
            if (this.txtNameNew.Text == "")
            {
                XtraMessageBox.Show("Tên chương trình đào tạo không thể bỏ trống");
                return;
            }
            Training_TrainingProgramInfo info = new Training_TrainingProgramInfo(strID);
            if (info.ExistInDatabase)
            {
                XtraMessageBox.Show("Mã chương trình đào tạo này đã tồn tại trong hệ thống.");
            }
            else
            {
                Training_TrainingProgramInfo infoTest = new Training_TrainingProgramInfo(this.m_trainingProgramInfo.Properties.Catalogue_MajorKey.Value, this.m_trainingProgramInfo.Properties.Catalogue_LevelKey.Value, this.m_trainingProgramInfo.Properties.Catalogue_TrainingTypeKey.Value, this.lueCatalogue_CourseNew.GetIntValue());
                if (!infoTest.ExistInDatabase)
                {
                    string strName = this.txtName.Text;
                    int iCourser = this.lueCatalogue_CourseNew.GetIntValue();
                    info.Properties.ID = strID;
                    info.Properties.Name = strName;
                    info.Properties.Catalogue_CourseKey = iCourser;
                    info.Properties.Catalogue_LevelKey = this.m_trainingProgramInfo.Properties.Catalogue_LevelKey;
                    info.Properties.Catalogue_MajorKey = this.m_trainingProgramInfo.Properties.Catalogue_MajorKey;
                    info.Properties.Catalogue_TrainingTypeKey = this.m_trainingProgramInfo.Properties.Catalogue_TrainingTypeKey;
                    info.Properties.GradCredits = this.m_trainingProgramInfo.Properties.GradCredits;
                    info.Properties.GradRule = this.m_trainingProgramInfo.Properties.GradRule;
                    //info.Properties.ID = this.m_trainingProgramInfo.Properties.MinimizeCredits;
                    info.Properties.MinimizeCredits = this.m_trainingProgramInfo.Properties.MinimizeCredits;
                    //info.Properties.Name = this.m_trainingProgramInfo.Properties.Name;
                    info.Properties.Note = this.m_trainingProgramInfo.Properties.Note;
                    info.Properties.OptionCredits = this.m_trainingProgramInfo.Properties.OptionCredits;
                    info.Properties.Purpose = this.m_trainingProgramInfo.Properties.Purpose;
                    info.Properties.ScoreType = this.m_trainingProgramInfo.Properties.ScoreType;
                    info.Properties.StudentType = this.m_trainingProgramInfo.Properties.StudentType;
                    info.Properties.Time = this.m_trainingProgramInfo.Properties.Time;
                    info.Properties.TotalCredits = this.m_trainingProgramInfo.Properties.TotalCredits;
                    //info.Properties.Training_TrainingProgramKey = this.m_trainingProgramInfo.Properties.Training_TrainingProgramKey;

                    info.Save();

                    int iTrainingProgrameKey = info.Properties.Training_TrainingProgramKey;
                    int result = this.m_dbTraining.m_DCSave.CopyTrainingProgram(this.m_trainingProgramInfo.Properties.Training_TrainingProgramKey, iTrainingProgrameKey);
                    if (result == 1)
                    {
                        XtraMessageBox.Show("Cấu hình thành công.");
                    }
                    else
                    {
                        XtraMessageBox.Show("Cấu hình thất bại.");
                    }
                }
                else
                {
                    XtraMessageBox.Show("Khóa học bạn chọn đã có chương trình đào tạo.");
                }
            }*/
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lueCatalogue_TrainingType_EditValueChanged(object sender, EventArgs e)
        {
        }

        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
        }
    }
}